package util;

import java.io.*;
import java.util.*;

public class Resolver {
    private static String filePath;
    private static FileReader fileReader;
    private static FileWriter fileWriter;
    private static File file;
    private static Set<String> novels;  //未处理的小说（不含后缀名）


    private Resolver() {
    }

    public static void resolver(String filePath) throws Exception {
        System.out.println("即将处理：   "+filePath);
        setFilePath(filePath);
        init();

    }

    private static void init() throws Exception {
        if (filePath == null || filePath.equals(""))
            throw new IOException("filePath is null");
        file = new File(filePath);
        novels = getFileName_novel();
        process();
    }

    private static Set<String> getFileName_novel() {
        File[] files = file.listFiles();
        Set<String> set = new HashSet<>(files.length);
        for (File f : files) {
            //文件
            if (f.isFile()) {
                String name = f.getName();
                set.add(name.substring(0, name.length() - 4));
            }
        }
        for (File f : files) {
            //文件夹
            if (f.isDirectory()) {
                set.remove(f.getName());
            }
        }
        return set;
    }

    public static String getFilePath() {
        return filePath;
    }

    private static void process() throws Exception {
        if (novels == null)
            throw new Exception("novels is null");
        Object[] array = novels.toArray();
        for (Object name : array) {
            //创建文件夹
            new File(filePath + "/" + name.toString()).mkdir();
            //对txt进行分解
            LinkedList<StringBuilder> ArticleList = ReaderAndSolve(name.toString());
            WriteArticleList(ArticleList, name.toString());
        }
    }
    //传入一个无后缀的文件夹名（txt）
    private static LinkedList<StringBuilder> ReaderAndSolve(String targetName) throws IOException {

        //分解目标的txt源
        String targetFileName = targetName + ".txt";
        fileReader = new FileReader(filePath + "/" + targetFileName);
        BufferedReader in = new BufferedReader(fileReader);
        StringBuilder content = new StringBuilder(10000);
        LinkedList<StringBuilder> ArticleList = new LinkedList<>();

        while (in.ready()) {
            String line = in.readLine();
            if (line.equals("------------")) {
                ArticleList.add(content);
                content = new StringBuilder(10000);
                continue;
            }
            content.append(line);
            content.append("<br>");
        }
        in.close();
        return ArticleList;
    }
    //传入一个无后缀的文件夹名（txt）
    private static void WriteArticleList(LinkedList<StringBuilder> ArticleList, String targetName) throws IOException {
        //分解后的txt要存的路径
        String path = filePath + "/" + targetName + "/";
        int cnt = 1;
        for (StringBuilder content : ArticleList) {
            //创建txt文件
            String targetPath = String.format("%s/%d.txt", path, cnt++);
            File txt = new File(targetPath);
            if (txt.exists())
                continue;
            fileWriter = new FileWriter(txt.getPath());
            BufferedWriter out = new BufferedWriter(fileWriter);
            out.write(content.toString());
            out.close();
        }

    }

    public static void setFilePath(String filePath) {
        Resolver.filePath = filePath;
    }

    public static void main(String[] args) throws Exception {
        resolver("/Users/tao/Desktop/java/Servlet/NetNovelStore/src/main/webapp/novelTXT");

    }
}
